﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Sim3D_practicum.Physics;

//opening to the hole is 1.75 to 2.25 * diameter ball. Side pockets wider than corner
//for now, we use 2*57 for all holes
namespace Sim3D_practicum.Components
{
    class Hole : Microsoft.Xna.Framework.DrawableGameComponent
    {
        static Texture2D art;
        const float RADIUS = 57;//in mm
        Vector3 position;
        Rectangle scaledPosition;
        Color colour;
        public Hole(Vector3 position, Game game) : base(game)
        {
            this.position = position;
            scaledPosition = new Rectangle();
            scaledPosition.X = Calculator.mmToPixel(position.X - RADIUS);
            scaledPosition.Y = Calculator.mmToPixel(position.Z - RADIUS);
            scaledPosition.Width = Calculator.mmToPixel(RADIUS*2);
            scaledPosition.Height = Calculator.mmToPixel(RADIUS*2);
            this.colour = new Color(255, 255, 255);
        }

        protected override void LoadContent()
        {
            if (art == null)
                art = Game.Content.Load<Texture2D>("Hole");
            base.LoadContent();
        }

        public void Draw(GameTime gameTime, SpriteBatch sprite)
        {
            sprite.Begin( );
            sprite.Draw(art, scaledPosition, colour);
            sprite.End( );
            base.Draw(gameTime);
        }

        public bool collide( Ball b )
        {
            //horizontally, the middle of the ball needs to be in the hole for it to start slipping in, so not Ball.Radius+hole.raduis
            //when balls lay on the table instead of in, substract height
            //if(Vector3.DistanceSquared(Vector3.Subtract(b.Position, new Vector3(0, Ball.RADIUS, 0)), position) <= Hole.RADIUS * Hole.RADIUS)
            if (Vector3.DistanceSquared(b.Position, position) <= Hole.RADIUS * Hole.RADIUS)
            {
                b.setVelocityX( 0 );
                b.setVelocityZ( 0 );
                return true;
            }
            else
                return false;
        }
    }
}
